﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;

namespace Authority
{
    /// <summary>
    /// 页面视图
    /// </summary>
    [Serializable]
    public class View
    {
        /// <summary>
        /// 字段信息
        /// </summary>
        private FieldInfo valueField;

        /// <summary>
        /// 页面枚举值
        /// </summary>
        public ViewEnum Value { get; set; }

        /// <summary>
        /// 页面视图名称
        /// </summary>     
        public string Name
        {
            get
            {
                var display = Attribute.GetCustomAttribute(this.valueField, typeof(DisaplyAttribute)) as DisaplyAttribute;
                return display == null ? null : display.Name;
            }
        }

        /// <summary>
        /// 所在组
        /// </summary>
        public string Group
        {
            get
            {
                var display = Attribute.GetCustomAttribute(this.valueField, typeof(DisaplyAttribute)) as DisaplyAttribute;
                return display == null ? null : display.Group;
            }
        }

        /// <summary>
        /// 连接
        /// </summary>
        public string Link
        {
            get
            {
                var link = Attribute.GetCustomAttribute(this.valueField, typeof(LinkAttribute)) as LinkAttribute;
                var value = link == null ? this.Value.ToString().ToLower() : link.Value;
                if (value != null)
                {
                    value = value.ToLower();
                }
                return value;
            }
        }

        /// <summary>
        /// 获取是否是菜单
        /// </summary>
        public bool IsMenu
        {
            get
            {
                return Attribute.IsDefined(this.valueField, typeof(NonMenuAttribute)) == false;
            }
        }
        /// <summary>
        /// 页面视图是否可用
        /// </summary>
        public bool Enable { get; set; }

        /// <summary>
        /// 页面视图包含的功能 
        /// </summary>
        public List<Action> AcitonList { get; set; }

        /// <summary>
        /// 构造器
        /// </summary>
        public View(ViewEnum value)
        {
            this.Value = value;
            this.valueField = typeof(ViewEnum).GetField(value.ToString());
            this.AcitonList = new List<Action>();
        }

        /// <summary>
        /// 获取页面是否拥有某种功能
        /// </summary>
        /// <param name="action">功能</param>
        /// <returns></returns>
        public bool Can(ActionEnum action)
        {
            var value = this.AcitonList.Find(item => item.Value == action);
            return value != null && value.Enable;
        }

        /// <summary>
        /// 文字显示
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            return string.Format("{0}/{1}", this.Group, this.Name);
        }

        /// <summary>
        /// 获取所有模块
        /// </summary>
        /// <returns></returns>
        public static List<View> All()
        {
            return typeof(ViewEnum)
                .GetFields(BindingFlags.Static | BindingFlags.Public)
                .Select((item, i) => new View((ViewEnum)i))
                .ToList();
        }
    }

}
